package com.xxxx.manager.controller;


import com.xxxx.common.result.BaseResult;
import com.xxxx.common.result.FileResult;
import com.xxxx.manager.service.GoodsCategoryService;
import com.xxxx.manager.service.UploadService;
import com.xxxx.manager.utils.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("fileUpload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @RequestMapping("save")
    @ResponseBody
    public FileResult upload(@RequestParam MultipartFile file) {


        return uploadService.upload(file);

        /*System.out.println(file);
        FileResult result = new FileResult();
        if (file.isEmpty()) {
            result.setError("Error");
            result.setMessage("上传失败");
            return result;
        }

        try {
            byte[] bytes = file.getBytes();
            String imageName = UUID.randomUUID().toString();


            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
                result.setSuccess("Success");
                result.setMessage("文件上传成功");

                String str="http://"+url;//将地址拼接完整
                //System.out.println(str);
                result.setFileUrl(str);

               *//* System.out.println(url);
                adminService.updatetu(url,request);*//*
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } catch (IOException e) {

            result.setError("Error");
            result.setMessage("上传失败");
            return result;
        }*/

    }
}
